package com.lzq.badminton.utils;

import com.lzq.badminton.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @Author 三更  B站： https://space.bilibili.com/663528522
 */
public class SecurityUtils
{

    /**
     * 获取用户
     **/
//    public static WxAppletAuthenticationToken getLoginUser()
//    {
//        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//        Object principal = authentication.getPrincipal();
////        Object principal = getAuthentication().getPrincipal();
//        System.out.println("#############################");
//        System.out.println(principal);
//        if (principal instanceof WxAppletAuthenticationToken) {
//            System.out.println("嗷呜!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
//        }
//        return (WxAppletAuthenticationToken) getAuthentication().getPrincipal();
//    }

    /**
     * 获取Authentication
     */
    public static Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    public static String getOpenId() {
        Authentication authentication = getAuthentication();
        String openId = (String) authentication.getCredentials();
        return openId;
    }

    public static Long getUserId() {
        Authentication authentication = getAuthentication();
        Long userId = (Long) authentication.getPrincipal();
        return userId;
    }
}